package org.test.dao.impl;

import org.test.dao.PizzaDAO;
import org.test.domain.Order;
import org.test.domain.Pizza;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PizzaDAOImpl implements PizzaDAO {

    @Override
    public void create(Pizza pizza) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(pizza);
            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public void delete(Long pizzaId) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            Pizza pizza = read(pizzaId);
            em.remove(pizza);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public Pizza read(Long pizzaId) {
        EntityManager em = null;
        Pizza pizza = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            pizza = (Pizza) em.find(Pizza.class, pizzaId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return pizza;
    }

    @Override
    public List<Pizza> readAll() throws SQLException {
        EntityManager em = null;
        List<Pizza> pizzas = new ArrayList<Pizza>();
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            TypedQuery<Pizza> query = em.createQuery("SELECT pizza FROM Pizza pizza ", Pizza.class);
            pizzas = query.getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return pizzas;
    }

    @Override
    public List<Order> getPizzaOrders(Long pizzaId) throws SQLException {
        Pizza pizza1;
        EntityManager em = null;
        List<Order> orders = new ArrayList<Order>();
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            pizza1 = (Pizza) em.find(Pizza.class, pizzaId);
            orders = pizza1.getOrders();
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return orders;
    }

    @Override
    public void update(Long pizzaId, Pizza pizza) throws SQLException {
        Pizza pizza1;
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            pizza1 = (Pizza) em.find(Pizza.class, pizzaId);
            pizza1.setComposition(pizza.getComposition());
            pizza1.setCost(pizza.getCost());
            pizza1.setMass(pizza.getMass());
            pizza1.setName(pizza.getName());
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }
}
